import 'package:flutter/widgets.dart';

class PaginationListener extends StatelessWidget {
  const PaginationListener({
    super.key,
    this.distance = 200,
    this.depth = 0,
    required this.child,
    required this.onNextPage,
  });

  final VoidCallback onNextPage;
  final double distance;
  final double depth;
  final Widget child;

  bool _onScroll(ScrollNotification notification) {
    final delta =
        notification.metrics.maxScrollExtent - notification.metrics.pixels;
    if (notification.depth <= depth && delta < distance) {
      onNextPage();
    }
    return false;
  }

  @override
  Widget build(BuildContext context) {
    return NotificationListener(
      onNotification: _onScroll,
      child: child,
    );
  }
}
